package com.hns.can.service.web;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.service.service.DriverExamService;
import com.hns.can.service.service.EnergySavingService;
import com.hns.can.service.vo.DriverCommentParamsVo;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.vo.CommonParamsVo;
import com.hns.can.common.web.BaseController;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

/**
 * 驾驶员能耗考核Controller
 * @author:zhangzq
 * @email:
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/energySaving")
public class EnergySavingController extends BaseController {

    @Autowired
    private EnergySavingService energySavingService;
    @Autowired
    private DriverExamService driverExamService;

    final private String[] headerNames= {"排名","得分","司机","营运线路","所属机构","运行时长","运行里程(KM)","运行能耗(L|M³)","百公里能耗(L/100KM)","目标能耗(L|M³)","能耗百分比(%)","线路平均能耗(L|M³)","是否节能"};
    final private String[] headerFields = {"allRank","score","drvName", "lineName", "organName", "lastingTime" ,"totalMile", "totalEnerCons", "hundKmEnerCons", "trgtEnerCons", "enerPercent", "lineAvrgEnerCons", "wthrEnersav" };

    /**
     * 分页查询车辆能耗考核
     * @param vo
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/searchEnergySaving")
    @ResponseBody
    public Object searchEnergySaving(CommonParamsVo vo)throws Exception{
        SysUserEntity user=this.getCurrentUser().getUser();
        driverExamService.initParams(vo,user,"T_COMM_ENER_CONS_STAT_INFO");
        driverExamService.getReportDate(vo.getBeginTime(), vo.getReportType(), vo);
        Pagination page = energySavingService.searchEnergySaving(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }

    /**
     * 获取驾驶员车辆能耗考核评语
     * @param vo
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/getEnerComments")
    @ResponseBody
    public Object getEnerComments(DriverCommentParamsVo vo) throws Exception {
        //数组：{评语，评价级别}
        SysUserEntity user=this.getCurrentUser().getUser();
        List<String> list = driverExamService.getComments(vo,user, new String[]{"getCommentContent4Ena","getCommentGrade4Ena"});
        return this.getReturnResult(list);
    }

    /**
     * 导出驾驶员车辆能耗考核Excel文档
     * @param vo
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/exportEnergySavingExcel")
    public void exportEnergySavingExcel(CommonParamsVo vo,HttpServletResponse response)throws Exception {
        List<Object> list = getEnergySavingFileData(vo);
        ExportUtil.exportFiles(Arrays.asList(headerNames), Arrays.asList(headerFields), list, response, "excel", URLEncoder.encode("车辆能耗考核", "UTF-8") + DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "车辆能耗考核数据");
    }

    /**
     * 导出驾驶员车辆能耗考核Pdf文档
     * @param vo
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/exportEnergySavingPdf")
    public void exportEnergySavingPdf(CommonParamsVo vo,HttpServletResponse response)throws Exception {
        List<Object> list = getEnergySavingFileData(vo);
        ExportUtil.exportFiles(Arrays.asList(headerNames), Arrays.asList(headerFields), list, response, "pdf", URLEncoder.encode("车辆能耗考核", "UTF-8") + DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "车辆能耗考核数据");
    }

    /**
     * 获取驾驶员车辆能耗考核
     * @param vo
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    private List<Object> getEnergySavingFileData(CommonParamsVo vo)throws Exception{
        SysUserEntity user=this.getCurrentUser().getUser();
        driverExamService.initParams(vo, user, "T_COMM_ENER_CONS_STAT_INFO");
        driverExamService.getReportDate(vo.getBeginTime(), vo.getReportType(), vo);
        List<Object> list = energySavingService.getAllEnergySaving(vo);
        if (PubUtil.isEmpty(list)) {
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        return list;
    }
}
